package com.leyou.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author: 黑马程序员
 **/
public class JsonUtils {

    public static final ObjectMapper mapper = new ObjectMapper();

    private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);

    public static String toString(Object obj) {
        if (obj == null) {
            return null;
        }
        if (obj.getClass() == String.class) {
            return (String) obj;
        }
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            logger.error("json序列化出错：" + obj, e);
            return null;
        }
    }

    public static <T> T toBean(String json, Class<T> tClass) {
        try {
            return mapper.readValue(json, tClass);
        } catch (IOException e) {
            logger.error("json解析出错：" + json, e);
            return null;
        }
    }

    public static <E> List<E> toList(String json, Class<E> eClass) {
        try {
            return mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, eClass));
        } catch (IOException e) {
            logger.error("json解析出错：" + json, e);
            return null;
        }
    }

    public static <K, V> Map<K, V> toMap(String json, Class<K> kClass, Class<V> vClass) {
        try {
            return mapper.readValue(json, mapper.getTypeFactory().constructMapType(Map.class, kClass, vClass));
        } catch (IOException e) {
            logger.error("json解析出错：" + json, e);
            return null;
        }
    }

    public static <T> T nativeRead(String json, TypeReference<T> type) {
        try {
            return mapper.readValue(json, type);
        } catch (IOException e) {
            logger.error("json解析出错：" + json, e);
            return null;
        }
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    static class User{
        private String name;
        private int age;
    }

    public static void main(String[] args) {
        // 字符串的序列化
        User user = new User("纯情小飞飞", 16);
        // bean => json字符串
        String json = toString(user); // {"name":"纯情小飞飞","age":16}
        System.out.println(json);
        // list => json字符串  :  [{"name":"纯情小鸭鸭","age":18},{"name":"纯情小猪猪","age":26}]
        List<User> userList = Arrays.asList(new User("纯情小鸭鸭", 18), new User("纯情小猪猪", 26));
        json = toString(userList);
        System.out.println(json);
        // 反序列化
        json = "{\"name\":\"纯情小飞飞\",\"age\":16}";
        // 反序列化成对象
        User user1 = toBean(json, User.class);
        System.out.println("对象：" + user1);
        // 对list进行反序列化
        json = "[{\"name\":\"纯情小鸭鸭\",\"age\":18},{\"name\":\"纯情小猪猪\",\"age\":26}]";
        List<User> users = toList(json, User.class);
        System.out.println("输出集合：" + users);
        // tomap
        json = "{\"name\":\"aaaa\",\"age\":\"16\",\"gender\":\"男\"}";
        Map<String, Object> map = toMap(json, String.class, Object.class);
        System.out.println("输出map="+map);

        // 讲一个比较变态方法：  通过反射获取 TypeReference 类上的泛型
        json = "{\"person1\":[{\"name\":\"纯情小鸭鸭\",\"age\":18},{\"name\":\"纯情小猪猪\",\"age\":26}],\"person2\":[{\"name\":\"乔碧萝\",\"age\":38},{\"name\":\"贝吉塔\",\"age\":266}]}";
        Map<String, List<User>> stringListMap = nativeRead(json, new TypeReference<Map<String, List<User>>>() {
        });
        System.out.println("转成map：变态方法："+json);
    }

}
